import React, {createContext, useContext, useEffect, useState} from "react";
import Header from "./components/Header";
import Footer from "./components/Footer";
import './App.css';
import element from './routes'
import {useRoutes} from "react-router-dom";
import zhCN from 'antd/es/locale/zh_CN';
import {ConfigProvider} from 'antd';
import http from "./util/http";

const AppContext = createContext();

export const EnvEnums = {
    local: 'local',
    test: 'test',
    prod: 'production'
};
export function AppProvider({ children }) {
    const [selectTab, setSelectTab] = useState('总院动态'); // 默认值
    const [routeHistory, setRouteHistory] = useState([
        '/home','/home'
    ]);
    const host = window.location.host;

    let currEnv = ''
    if (host.startsWith('localhost')){
        currEnv = 'local'
    } else if (host.startsWith('172.22.160.246')){
        currEnv = 'test'
    }else{
        currEnv = 'production'
    }
    // currEnv = 'production'
    console.log("env = "+currEnv);
    const [selectLanguage, setSelectLanguage] = useState('cn'); // 默认值
    const [env, setEnv] = useState(currEnv); // 默认值

    return (
        <AppContext.Provider value={{
            selectTab, setSelectTab,
            selectLanguage, setSelectLanguage,
            env,setEnv,routeHistory,setRouteHistory
        }}>
            {children}
        </AppContext.Provider>
    );
}

// 自定义 hook 方便使用
export function useAppContext() {
    return useContext(AppContext);
}
function App() {
    let routes = useRoutes(element);
    useEffect(() => {
        http.doGet('/gray/getFlag', {}).then(res => {
            if (res.status === 200&&res.enable) {
                const elmain = document.getElementById("wb-main");
                elmain.classList.add("my-set-gray");
            }
        })
    })
    return (
        <AppProvider>
            <ConfigProvider locale={zhCN}>
                <div className="main" id="wb-main">
                    <Header/>
                    {routes}
                    <Footer/>
                </div>
            </ConfigProvider>
        </AppProvider>
    );
}

export default App;
